
using System;
using System.Numerics;
using Furion.DistributedIDGenerator;

namespace SB2.Core;

[SugarTable("sbsysobjects")]
public class SysObject : EntityHasUser
{
    [SugarColumn(IsPrimaryKey = true, ColumnName = "ObjectId", ColumnDescription = "主键")]
    public long ObjectId { get; set; }
    [SugarColumn(ColumnDescription = "名称", IsNullable = false)]
    public string ObjName { get; set; }
    [SugarColumn(ColumnDescription = "默认值")]
    public string DefultValue { get; set; }
    [SugarColumn(ColumnDescription = "父级Id")]
    public long PId { get; set; }
    [SugarColumn(ColumnDescription = "数据类型")]
    public SysObjectType DataType { get; set; }
    [SugarColumn(ColumnDescription = "长度")]
    public int Length { get; set; }
    [SugarColumn(ColumnDescription = "是否主键？")]
    public bool Iskey { get; set; }
    [SugarColumn(ColumnDescription = "是否可空")]
    public bool NotNull { get; set; }
    [SugarColumn(ColumnDescription = "描述")]
    public string Des { get; set; }


    public SysObject GetNewId()
    {
        ObjectId = BitConverter.ToInt64(IDGen.NextID().ToByteArray());
        return this;
    }
    public string GetSql()
    {
        var type = "";
        switch (DataType)
        {
            case SysObjectType.sbit:
                type = " BIT ";
                break;
            case SysObjectType.sint:
                type = " INT ";
                break;
            case SysObjectType.sBigInteger:
                type = " BIGINT ";
                break;
            case SysObjectType.sVARCHAR:
                var len = this.Length > 0 ? this.Length : 128;
                type = $" VARCHAR({len}) ";
                break;
            case SysObjectType.sDateTime:
                type = $" datetime ";
                break;
            default:
                type = " VARCHAR(128) ";
                break;
        }
        var isnull = NotNull ? " NOT NULL " : " NULL ";
        var defultValue = string.IsNullOrEmpty(DefultValue) ? "" : $" DEFAULT {DefultValue} ";
        return $"f{ObjectId} {type} {isnull} {defultValue} ";
    }
    /// <summary>
    /// 获取数据库字段名称 /表名称
    /// 表名称u*
    /// 字段f*
    /// </summary>
    /// <returns></returns>
    public string GetName()
    {
        return PId == 0 ? $"u{ObjectId}" : $"f{ObjectId}"; ;
    }
}

public enum SysObjectType
{
    sbit = 0,
    sint = 1,
    sBigInteger = 2,
    sVARCHAR = 3,
    sDateTime = 4,


}
public class SysObjectDto : DtoHasUser
{
    [DisplayDto(Hide = true)]
    public long ObjectId { get; set; }
    [DisplayDto(DisplayName = "表名", Seachable = true, DataType = DisplayDtoDataType.Text)]
    public string ObjName { get; set; }
    [DisplayDto(DisplayName = "描述", Seachable = true, DataType = DisplayDtoDataType.Text)]
    public string Des { get; set; }
}
public class SysObjectFieldDto : DtoHasUser
{
    [DisplayDto(Hide = true)]
    public long ObjectId { get; set; }
    [DisplayDto(DisplayName = "列名", Seachable = true, DataType = DisplayDtoDataType.Text)]
    public string ObjName { get; set; }
    [DisplayDto(DisplayName = "描述", Seachable = true, DataType = DisplayDtoDataType.Text)]
    public string Des { get; set; }
    [DisplayDto(DisplayName = "长度", Seachable = true, DataType = DisplayDtoDataType.Number)]
    public int Length { get; set; }

    [DisplayDto(DisplayName = "不可空", Seachable = true, DataType = DisplayDtoDataType.Bool)]
    public bool NotNull { get; set; }
}
public class SysObjectCreateDto
{
    /// <summary>
    /// 表名
    /// </summary>
    /// <value></value>
    [Required]
    public long TableId { get; set; }
    /// <summary>
    /// 数据内容
    /// </summary>
    /// <value></value>
    public Dictionary<string, object> Data { get; set; }

}

public class SysObjectNewTableDto
{
    /// <summary>
    /// 表名
    /// </summary>
    /// <value></value>
    [Required]
    [DisplayDto(DisplayName = "表名", DataType = DisplayDtoDataType.Text)]
    public string ObjName { get; set; }

    /// <summary>
    /// 描述
    /// </summary>
    /// <value></value>
    [DisplayDto(DisplayName = "描述", DataType = DisplayDtoDataType.Text)]
    public string Des { get; set; }
    /// <summary>
    /// 字段
    /// </summary>
    /// <value></value>
    public List<SysObjectNewFiledDto> Fileds { get; set; }
}

public class SysObjectNewFiledDto
{
    /// <summary>
    /// 表名
    /// </summary>
    /// <value></value>

    public long PId { get; set; }
    /// <summary>
    /// 字段名称
    /// </summary>
    /// <value></value>
    [Required]
    public string ObjName { get; set; }
    /// <summary>
    /// 默认值
    /// </summary>
    /// <value></value>
    public string DefultValue { get; set; }
    /// <summary>
    /// 数据类型
    /// </summary>
    /// <value></value>
    public SysObjectType DataType { get; set; }
    /// <summary>
    /// 长度
    /// </summary>
    /// <value></value>
    public int Length { get; set; }
    /// <summary>
    /// 是否可空
    /// </summary>
    /// <value></value>
    public bool IsNull { get; set; }
    /// <summary>
    /// 描述
    /// </summary>
    /// <value></value>
    public string Des { get; set; }
}

